// 轮播图
    var items = document.getElementsByClassName('item');    
    var goPreBtn = document.getElementById('goPre');
    var goNextBtn = document.getElementById('goNext');
    var points =document.getElementsByClassName('point');//获取所有img和point
    var time = 0;//定时器图片跳转参数
    var index = 0;//index表示第几张图片在展示 第index张图片有active这个类名
    var clearActive = function(){    	
        for(var i = 0;i<items.length;i++){       	
            items[i].className='item';	        
           }       
        for(var i = 0;i<points.length;i++){
            points[i].className='point';
           }       
        }

// 跳转到登录
var exit= document.querySelector(".top5")
exit.addEventListener("click",function(){
    $.ajax({
        type: "post",
        url: "/User/loginOut",
        // data: {
        // },
        success: function (data) {
            // alert("退出")
            window.location.href="/";

        },error: function () {
            console.log("調取接口失敗")
        }
    })
})
    //定义一个函数clearActive,i从0开始,小于items中img的个数,i++,图片依次往后,i从0开始,
    //          小于point点的个数,点依次往后.
    var goIndex = function(){    	
        clearActive();//执行clearActive函数,	    
        //console.log(index)	//打印
        points[index].className='point active';	    
        items[index].className='item active';	    
    }
    var goNext = function(){
        if(index<2){	
            index++;  
        }else{  	
                index = 0;
           }
        goIndex();
    }
     //点击下一个按钮执行goNext函数，当index小于图片个数(index从零开始所以为2),i++;当index到最后一张图片时,index=0,到第一张图片
    var goPre = function(){
        if(index ==0){
            index=2;
        }else{
            index--;
    }	   
        goIndex();    
       } 
       //点击上一个按钮时执行goPre函数，当index为第一张图片时，index变为2展示最后一张图片，其他时候index--。
       document.getElementById('goPre').addEventListener("click",function(){
            goPre();
       })
    document.getElementById("goNext").addEventListener("click",function(){
            goNext();
    })
    for(var i=0;i<points.length;i++){
    points[i].addEventListener('click',function(){
        var pointIndex=this.getAttribute('data-index');
        index=pointIndex;
        goIndex();
        time = 0;
    })
    }
    /*实现自动播放图片的效果*/
    setInterval(function(){
            goNext();
    },4000)//2000ms跳转一次
//回到顶部
    var backTop=document.getElementById("backTop"),
    topH,//滚动条距离顶部高度
    backTopSpeed,//滚动速度
    backTopTimer;//定时器
    backTop.addEventListener('click',function(){
        var backTopTimer = setInterval(function(){
            topH = document.documentElement.scrollTop || document.body.scrollTop;
            backTopSpeed = Math.floor(-topH/8);//取整
            if(topH>0){
                document.documentElement.scrollTop = document.body.scrollTop = topH+backTopSpeed;
            }else{
                clearInterval(backTopTimer);
            }
        },30);
    });
    var backTopTimer = setInterval(function(){
        topH = document.documentElement.scrollTop || document.body.scrollTop;
        backTopSpeed = Math.floor(-topH/8);//取整
        if(topH>2){
            gotop.style.display="block";
        }else{
            gotop.style.display="none";
        }
    })
    // 点击出现隐藏的内容
    var hid_huawei=document.getElementById("hid_huawei")
    var huawei=document.getElementById("huawei") 
    var hid_vivo=document.getElementById("hid_vivo")
    var vivo=document.getElementById("vivo")
    var hid_oppo=document.getElementById("hid_oppo")
    var oppo=document.getElementById("oppo")
    var hid_iphon=document.getElementById("hid_iphon")
    var iphon=document.getElementById("iphon")
    var hid_leno=document.getElementById("hid_leno")
    var leno=document.getElementById("leno")
    var hid_hp=document.getElementById("hid_hp")
    var hp=document.getElementById("hp")
    var hid_dr=document.getElementById("hid_dr")
    var dr=document.getElementById("dr")
    var hid_jx=document.getElementById("hid_jx")
    var jx=document.getElementById("jx")
    var hid_mi=document.getElementById("hid_mi")
    var mi=document.getElementById("mi")
    var hid_more=document.getElementById("hid_more")
    var more=document.getElementById("more")
    showhuawei=function (){
        if(hid_huawei.style.display=="none"){
            hid_huawei.style.display="block";
            huawei.style.color="red";
        }else{
            hid_huawei.style.display="none";
            huawei.style.color="black";
        }
    }
    showvivo=function(){
        if(hid_vivo.style.display=="none"){
            hid_vivo.style.display="block";
            vivo.style.color="red";
        }else{
            hid_vivo.style.display="none";
            vivo.style.color="black"
        }
    }
    showoppo=function(){
        if(hid_oppo.style.display=="none"){
            hid_oppo.style.display="block";
            oppo.style.color="red";
        }else{
            hid_oppo.style.display="none";
            oppo.style.color="black"
        }
    }
    showiphon=function(){
        if(hid_iphon.style.display=="none"){
            hid_iphon.style.display="block";
            iphon.style.color="red";
        }else{
            hid_iphon.style.display="none";
            iphon.style.color="black";
        }
    }
    showleno=function(){
        if(hid_leno.style.display=="none"){
            hid_leno.style.display="block";
            leno.style.color="red";
        }else{
            hid_leno.style.display="none";
            leno.style.color="black";
        }
    }
    showhp=function(){
        if(hid_hp.style.display=="none"){
            hid_hp.style.display="block";
            hp.style.color="red";
        }else{
            hid_hp.style.display="none";
            hp.style.color="black"
        }
    }
    showdr=function(){
        if(hid_dr.style.display=="none"){
            hid_dr.style.display="block";
            dr.style.color="red";
        }else{
            hid_dr.style.display="none";
            dr.style.color="black";
        }
    }
    showjx=function(){
        if(hid_jx.style.display=="none"){
            hid_jx.style.display="block";
            jx.style.color="red";
        }else{
            hid_jx.style.display="none";
            jx.style.color="black";
        }
    }
    showmi=function(){
        if(hid_mi.style.display=="none"){
            hid_mi.style.display="block";
            mi.style.color="red";
        }else{
            hid_mi.style.display="none";
            mi.style.color="black";
        }
    }
    showmore=function(){
        if(hid_more.style.display=="none"){
            hid_more.style.display="block";
        }else{
            hid_more.style.display="none";
        }
    }
///////加载数据
var lookMore = document.getElementById("lookMore")
var things = document.getElementById("things")
$(document).ready(function(){
    $.ajax({
        type:'get',
        url:"/getIndex",
        success:function(data){
            console.log(data);
            console.log(data.t.Brand[0][0].goodsId);
            console.log(data.t.Brand[0].length)
            for(i=0;i<9;i++){
                for(j=0;j<data.t.Brand[i].length;j++){
                    things.innerHTML+=
                        `<div class="thing" onclick="fun()">
                        <div class="photo" name="${data.t.Brand[i][j].goodsId}"><img src="${data.t.Brand[i][j].goodsImg}" class="picture" name="${data.t.Brand[i][j].goodsId}"></div>
                        <h4 class="function" name="${data.t.Brand[i][j].goodsId}">${data.t.Brand[i][j].goodsName}</h4>
                        <p class="shop" name="${data.t.Brand[i][j].shopId}">${data.t.Brand[i][j].shopName}</p>
                        <p class="price" name="${data.t.Brand[i][j].goodsId}">${data.t.Brand[i][j].goodsPrice}</p>
                        </div>`
                }
            };
            $(".function").each(function(){
                var str = $(this).html();
                var subStr = str.substring(0,25);
                var data = subStr+(str.length>25?'···':'');
                $(this).html(data);
            })
        },
        error:function(){
            alert("请求失败！")
        }
    })
})
var hwProduct = document.getElementById("hwProduct")
$(document).ready(function(){
    $.ajax({
        type:'get',
        url:"/getIndex",
        success:function(data){
            console.log(data);
            console.log(data.t.Brand[0].length)
            for(i=0;i<data.t.Brand[0].length;i++){
                hwProduct.innerHTML+=
                    `<div class="hwPort">
                     <p class="goodsName" name="${data.t.Brand[0][i].goodsId}">${data.t.Brand[0][i].goodsName}</p>
                     </div>`
            }
        },
        error:function(){
            console.log(error)
        }
    })
})
var vivoProduct = document.getElementById("vivoProduct")
$(document).ready(function(){
    $.ajax({
        type:'get',
        url:"/getIndex",
        success:function(data){
            for(i=0;i<data.t.Brand[1].length;i++){
                vivoProduct.innerHTML+=
                    `<div class="vivoPort">
                     <p class="goodsName" name="${data.t.Brand[1][i].goodsId}">${data.t.Brand[1][i].goodsName}</p>
                     </div>`
            }
        },
        error:function(){
            console.log(error)
        }
    })
})
var oppoProduct = document.getElementById("oppoProduct")
$(document).ready(function(){
    $.ajax({
        type:'get',
        url:"/getIndex",
        success:function(data){
            for(i=0;i<data.t.Brand[2].length;i++){
                oppoProduct.innerHTML+=
                    `<div class="oppoPort">
                     <p class="goodsName" name="${data.t.Brand[2][i].goodsId}">${data.t.Brand[2][i].goodsName}</p>
                     </div>`
            }
        },
        error:function(){
            console.log(error)
        }
    })
})
var iphonProduct = document.getElementById("iphonProduct")
$(document).ready(function(){
    $.ajax({
        type:'get',
        url:"/getIndex",
        success:function(data){
            for(i=0;i<data.t.Brand[3].length;i++){
                iphonProduct.innerHTML+=
                    `<div class="iphonPort">
                     <p class="goodsName" name="${data.t.Brand[3][i].goodsId}">${data.t.Brand[3][i].goodsName}</p>
                     </div>`
            }
        },
        error:function(){
            console.log(error)
        }
    })
})
var lenoProduct = document.getElementById("lenoProduct")
$(document).ready(function(){
    $.ajax({
        type:'get',
        url:"/getIndex",
        success:function(data){
            for(i=0;i<data.t.Brand[4].length;i++){
                lenoProduct.innerHTML+=
                    `<div class="lenoPort">
                     <p class="goodsName" name="${data.t.Brand[4][i].goodsId}">${data.t.Brand[4][i].goodsName}</p>
                     </div>`
            }
        },
        error:function(){
            console.log(data)
        }
    })
})
var hpProduct = document.getElementById("hpProduct")
$(document).ready(function(){
    $.ajax({
        type:'get',
        url:"/getIndex",
        success:function(data){
            for(i=0;i<data.t.Brand[5].length;i++){
                hpProduct.innerHTML+=
                    `<div class="hpPort">
                     <p class="goodsName" name="${data.t.Brand[5][i].goodsId}">${data.t.Brand[5][i].goodsName}</p>
                     </div>`
            }
        },
        error:function(){
            console.log(data)
        }
    })
})
var drProduct = document.getElementById("drProduct")
$(document).ready(function(){
    $.ajax({
        type:'get',
        url:"/getIndex",
        success:function(data){
            for(i=0;i<data.t.Brand[6].length;i++){
                drProduct.innerHTML+=
                    `<div class="drPort">
                     <p class="goodsName" name="${data.t.Brand[6][i].goodsId}">${data.t.Brand[6][i].goodsName}</p>
                     </div>`
            }
        },
        error:function(){
            console.log(data)
        }
    })
})
var jxProduct = document.getElementById("jxProduct")
$(document).ready(function(){
    $.ajax({
        type:'get',
        url:"/getIndex",
        success:function(data){
            for(i=0;i<data.t.Brand[7].length;i++){
                jxProduct.innerHTML+=
                    `<div class="jxPort">
                     <p class="goodsName" name="${data.t.Brand[7][i].goodsId}">${data.t.Brand[7][i].goodsName}</p>
                     </div>`
            }
        },
        error:function(){
            console.log(data)
        }
    })
})
var miProduct = document.getElementById("miProduct")
$(document).ready(function(){
    $.ajax({
        type:'get',
        url:"/getIndex",
        success:function(data){
            for(i=0;i<data.t.Brand[8].length;i++){
                miProduct.innerHTML+=
                    `<div class="miPort">
                     <p class="goodsName" name="${data.t.Brand[8][i].goodsId}">${data.t.Brand[8][i].goodsName}</p>
                     </div>`
            }
        },
        error:function(){
            console.log(data)
        }
    })
})
///////搜索框

/////搜索框搜索商品
var seekit = document.getElementById("seekit")
var seeksth = document.getElementById("seeksth")
var wrap = document.getElementById("wrap")
var things = document.getElementById("things")
var goods = document.getElementById("goods")
var shops = document.getElementById("shops")
goods.onclick = function(){
        goods.style.color="black";
        shops.style.color="white";
}
shops.onclick = function(){
    goods.style.color="white";
    shops.style.color="black";
    seekit.onclick=function(){
        var sth = seeksth.value;
        console.log(sth)
        $.ajax({
            type: 'post',
            data: {
                shopName: sth,
                pageNow: 1,
                pageSize: 1,
            },
            url: "/queryShops",
            success:function(data){
                if(sth==""){
                    alert("请输入要搜索的店铺名！")
                    return false;
                }else{
                    console.log(data);
                    wrap.style.display="none";
                    things.innerHTML="";
                    things.innerHTML+=`
                    <p class="shop" id="shop" name="${data.shopPage.list[0].shopId}">${data.shopPage.list[0].shopName}</p>
                    `
                }
            },


            error:function(){
                console.log(data)
            }
        })
    }


}


seekit.onclick=function(){
    var sth = seeksth.value;
        console.log(sth)
        $.ajax({
            type: 'post',
            data: {
                goodsName: sth,
                pageNow: 1,
                pageSize: 1,
            },
            url: "/queryGoods",
            success: function (data) {
                if(sth==""){
                    alert("请输入要搜索的商品名！")
                    return false;
                }else{
                    console.log(data);
                    wrap.style.display="none";
                    things.innerHTML="";
                    console.log(data.goodsPage.list[0].goodsName);
                    things.innerHTML+=
                        `<div class="thing">
                    <div class="photo"><img src="${data.goodsPage.list[0].goodsImg}" class="picture" name="${data.goodsPage.list[0].goodId}"></div>
                    <h4 class="function">${data.goodsPage.list[0].goodsName}</h4>
                    <p class="shop" name="${data.goodsPage.list[0].shopId}">${data.goodsPage.list[0].shopName}</p>
                    <p class="price">${data.goodsPage.list[0].goodsPrice}</p>
                    </div>`
                }
            },
            error:function(){
                console.log(data)
            }
        })
}



//////查看更多
var lookMore = document.getElementById("lookMore")
lookMore.onclick = function(){
        window.location.href="allGoods";
}
//////顶部按钮
var top1 = document.getElementById("top1")
var mone = document.getElementById("mone")
var content = document.getElementById("content")
var top2 = document.getElementById("top2")
var top3 = document.getElementById("top3")
var top4 = document.getElementById("top4")
var top0 = document.getElementById("top0")
var fg = localStorage.getItem("key");
console.log(fg)
if(fg==0){
    top0.style.display="none";
}else{
    top0.style.display="block";
}
top1.onclick = function(){
    var fg = localStorage.getItem("key");
    console.log(fg)
    if(fg==0){
        alert("您还没有登录，请您先登录！")
        window.location.href = "User/login";
    }else if(fg==2){
        window.location.href="/admin/login";
    }else if(fg==3){
        alert("管理员正在审核，请耐心等待！")
    }else if(fg==4){
        window.location.href="/Merchant/personShopCar"
    }else if(fg==1){
        var r=confirm("是否申请成为商家？");
        if (r==true) {
            content.style.display = "none";
            mone.style.display = "block";
        }else{
            window.location.href="/"
        }
    }
}
top2.onclick = function(){
    var fg = localStorage.getItem("key");
    console.log(fg)
        if(fg==0){
            alert("你还没有登录，请你先登录")
            window.location.href = "User/login";
        }else{
            window.location.href="/User/userInterface"
        }
    }
top3.onclick = function () {
    window.location.href="/User/myCarPage"
}
top4.onclick = function (){
    window.location.href="/User/login"
}
//////点击商品跳转到商品详情页
function fun(e) {
    var e=e || window.event;
    var target=e.target || e.srcElement;
    console.log(e.target);
    let Index=e.target.getAttribute("name");
    console.log(Index);
    var goodsId = Index;
    localStorage.setItem('name', goodsId);
    window.location.href="/goodsDetail";
}
///////点击店铺跳转到店铺
// $('things').on('click','#shop',function(){
// window.location.href="/Merchant/personShopCar"
// })
var classcar = document.getElementById("classcar")
classcar.onclick=function(){
    window.location.href="/User/myCarPage"
}
var no = document.getElementById("no")
var sure = document.getElementById("sure")
no.onclick = function(){
    content.style.display="block";
    mone.style.display="none";
}
sure.onclick = function(){
    content.style.display="block";
    mone.style.display="none";
}
var top5 = document.getElementById("top5")
top5.onclick = function(){
        let a = 0;
    localStorage.setItem('key', a);
        console.log(a)
}
//////点击轮播图跳转
var img1 = document.getElementById("img1")
var img2 = document.getElementById("img2")
var img3 = document.getElementById("img3")
img1.onclick=function () {
    let gg = 144
    localStorage.setItem("gg",gg)
    console.log(gg)
    window.location.href="/goodsDetail"
}
img2.onclick=function () {
    let gg = 131
    localStorage.setItem("gg",gg)
    console.log(gg)
    window.location.href="/goodsDetail"
}
img3.onclick=function () {
    let gg = 132
    localStorage.setItem("gg",gg)
    console.log(gg)
    window.location.href="/goodsDetail"
}
//发送商家申请
var sure = document.getElementById("sure")
var form = document.getElementById("form")
var  apply = document.getElementById("apply")
var think = document.getElementById("think")
var shopName = document.getElementById("shopName")
var userId = document.getElementById("userId")
sure.onclick=function () {
        form.style.display="block";
        content.style.display="none";
        mone.style.display="none";
        apply.onclick=function(){
            var shopname = shopName.value;
            var userid = userId.value;
            if(shopname != "") {
                var reg = /^[a-zA-Z]([-_a-zA-Z0-9]{6,10})$/;
                isok = reg.test(shopname);
                if (!isok) {
                    alert("店名不符合要求，请重新输入！");
                    return false;
                } else {
                    $.ajax({
                        type:"get",
                        data:{
                            shopName:shopname,
                            userId:userid,
                        },
                        url:"/Merchant/addShop",
                        success:function(data){
                            console.log(data);
                            alert("申请成功！请耐心等待审核")
                            window.location.href="/"
                        },
                        error:function () {
                            alert("申请失败！")
                        }
                    })
                }
            }

        }
}
think.onclick=function(){
    form.style.display="none";
    content.style.display="block";
}